package com.zzq.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zzq.constant.MessageConstant;
import com.zzq.entity.PageResult;
import com.zzq.entity.QueryPageBean;
import com.zzq.entity.Result;
import com.zzq.pojo.TravelGroup;
import com.zzq.service.TravelGroupService;
import com.zzq.service.TravelItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 幼儿猿大班
 * 2021/7/21
 */
@RestController
@RequestMapping("/travelGroup")
public class TravelGroupController {

    @Reference
    private TravelGroupService travelGroupService;

    @RequestMapping("/save")
    @PreAuthorize("hasAuthority('TRAVELGROUP_ADD')")
    public Result save(@RequestBody TravelGroup travelGroup, Integer[] travelItemIds){
        try {
            travelGroupService.save(travelGroup,travelItemIds);

        } catch (Exception e) {
            return new Result(false,MessageConstant.ADD_TRAVELGROUP_FAIL);
        }
        return new Result(true,MessageConstant.ADD_TRAVELGROUP_SUCCESS);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return travelGroupService.findPage(queryPageBean);
    }

    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('TRAVELGROUP_DELETE')")
    public Result delete(Integer id){
        try {
            travelGroupService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_TRAVELGROUP_FAIL);
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            TravelGroup travelGroup = travelGroupService.findById(id);
            return new Result(true,MessageConstant.QUERY_TRAVELGROUP_SUCCESS,travelGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_TRAVELGROUP_FAIL);
        }
    }

    @RequestMapping("/findTravelItemTravelGroupIds")
    public Result findTravelItemTravelGroupIds(Integer id){
        try {
            List<Integer> list= travelGroupService.findTravelItemTravelGroupIds(id);
            return new Result(true,MessageConstant.QUERY_TRAVELITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_TRAVELITEM_FAIL);
        }
    }

    @RequestMapping("/update")
    @PreAuthorize("hasAuthority('TRAVELGROUP_EDIT')")
    public Result update(@RequestBody TravelGroup travelGroup,Integer[] travelItemIds){
        try {
            travelGroupService.update(travelGroup,travelItemIds);
            return new Result(true,MessageConstant.EDIT_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_TRAVELGROUP_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<TravelGroup> travelGroups=travelGroupService.findAll();
            return new Result(true,MessageConstant.QUERY_TRAVELGROUP_SUCCESS,travelGroups);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_TRAVELGROUP_FAIL);
        }
    }
}
